import { useModel } from "umi";
import { message } from "antd";

const useDeletePosition = () => {
    const { graphqlFetch } = useModel("global");

    const deletePosition = async (opUuid, callback) => {
        let query = `query MyQuery($opUuid: UUID!) { a: allOrganizationInstitutionsMembers(condition: { opUuid: $opUuid }) { totalCount } }`;
        let res = await graphqlFetch({ query, variables: { opUuid } });
        if (res === null) return;
        if (res?.a?.totalCount === 0) {
            query = `mutation MyMutation($opUuid: UUID!) { deleteOrganizationPositionByOpUuid(input: { opUuid: $opUuid }) { clientMutationId } }`;
            res = await graphqlFetch({ query, variables: { opUuid } });
            if (res === null) return;
            message.success("成功删除职位信息");
            callback && callback();
        } else {
            message.warning("该职位下有成员，不能删除");
        }
    };

    return { deletePosition };
};

export default useDeletePosition;
